﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.Utils;
using FlyHorse.LIMS.WinApp.LanguageResource;
using System.Data.Entity;

namespace FlyHorse.LIMS.WinApp.DataManageForms.ReportProductForms
{
    public partial class ReportProductDetailForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.CustomerBll customerBll = new BLL.CustomerBll();
        BLL.ReportProductBll reportProductBll = new BLL.ReportProductBll();
        BLL.ReportProductResultBll reportProductResultBll = new BLL.ReportProductResultBll();
        private int _option;
        List<ReportProductResult> _resList = new List<ReportProductResult>();
        ReportProduct _report;
        Action _ShowAllData;
        OJCB4Entities db = new OJCB4Entities();
        public ReportProductDetailForm(Action ShowAllData)
        {
            InitializeComponent();
            _option = 1;
            this.Text = ResourceOverall.CreateStr;
            // textEditTypeOfPack.Text = "袋";
            _ShowAllData = ShowAllData;
        }

        public ReportProductDetailForm(ReportProduct report, Action ShowAllData)
        {
            InitializeComponent();
            _option = 2;
            _report = db.ReportProducts.Find(report.ReportProductID);
            this.Text = ResourceOverall.EditStr;
            InitEditForm(_report);
            _ShowAllData = ShowAllData;
        }

        private void ReportProductDetailForm_Load(object sender, EventArgs e)
        {
            //绑定 样品名称下拉框
            this.lookUpEditGoodsName.Properties.DataSource = goodsBll.GetProductGoodList();
            //绑定收货单位下拉框
            this.searchLookUpEdit_Customer.Properties.DataSource = customerBll.LoadEntities(m => true).ToList();

            if (_option == 1)
            {
                textEdit_userName.Text = LoginUserUtil.LoginUser.EmpName;
                // this.dataGridControl.DataSource = _resList;
            }

        }
        private void InitEditForm(ReportProduct report)
        {
            this.lookUpEditGoodsName.Enabled = false;
            this.searchLookUpEdit_Customer.Enabled = false;
            //填充数据
            this.lookUpEditGoodsName.EditValue = report.GoodsID;
            searchLookUpEdit_Customer.EditValue = report.CustomerID;
            if (report.DateProduced != null)
            {
                this.dateEdit_DateProduced.DateTime = Convert.ToDateTime(report.DateProduced);
            }
            this.textEdit_ReportProductNO.Text = report.ReportProductNO;
            textEdit_LotNo.Text = report.LotNo;
            textEditWeight.Text = report.Weight;
            textEditTypeOfPack.Text = report.TypeOfPack;
            if (report.ExPlant != null)
            {
                dateEditExPlant.DateTime = Convert.ToDateTime(report.ExPlant);
            }
            textEditShippedTo.Text = report.ShippedTo;
            textEdit_Remark.Text = report.Remark;
            textEdit_userName.Text = report.LoginUser.EmpName;
            _resList = report.ReportProductResults.ToList();
            this.dataGridControl.DataSource = _resList;
        }



        private void ButtonItem_save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //关闭编辑状态
            this.gridView1.CloseEditor();

            //判空
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                lookUpEditGoodsName.Focus();
                return;
            }
            if (this.dateEdit_DateProduced.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                dateEdit_DateProduced.Focus();
                return;
            }
            if (searchLookUpEdit_Customer.EditValue == null || searchLookUpEdit_Customer.Text == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                searchLookUpEdit_Customer.Focus();
                return;
            }


            if (_option == 1)
            {
                ReportProduct report = new ReportProduct();
                report.ReportProductID = Guid.NewGuid().ToString();

                report.CustomerID = Convert.ToString(this.searchLookUpEdit_Customer.EditValue);
                if (this.dateEdit_DateProduced.EditValue != null)
                {
                    report.DateProduced = this.dateEdit_DateProduced.DateTime;
                }
                if (dateEditExPlant.EditValue != null)
                {
                    report.ExPlant = this.dateEditExPlant.DateTime;
                }
                report.GoodsID = Convert.ToString(this.lookUpEditGoodsName.EditValue);
                report.LotNo = this.textEdit_LotNo.Text;
                report.Remark = this.textEdit_Remark.Text;
                report.ReportProductNO = this.textEdit_ReportProductNO.Text;
                report.ShippedTo = this.textEditShippedTo.Text;
                report.TypeOfPack = this.textEditTypeOfPack.Text;
                report.Weight = this.textEditWeight.Text;
                report.LoginUserID = LoginUserUtil.LoginUser.LoginUserID;
                report.AddDateTime = DateTime.Now;
                report.ReportProductResults = _resList;
                using (OJCB4Entities entities = new OJCB4Entities())
                {
                    entities.ReportProducts.Add(report);
                    var res = entities.SaveChanges() > 0;
                    if (res)
                    {
                        _ShowAllData();
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                        this.Close();
                    }
                }


            }
            else if (_option == 2)
            {
                _report.CustomerID = Convert.ToString(this.searchLookUpEdit_Customer.EditValue);
                if (this.dateEdit_DateProduced.EditValue != null)
                {
                    _report.DateProduced = this.dateEdit_DateProduced.DateTime;
                }
                if (dateEditExPlant.EditValue != null)
                {
                    _report.ExPlant = this.dateEditExPlant.DateTime;
                }
                _report.GoodsID = Convert.ToString(this.lookUpEditGoodsName.EditValue);
                _report.LotNo = this.textEdit_LotNo.Text;
                _report.Remark = this.textEdit_Remark.Text;
                _report.ReportProductNO = this.textEdit_ReportProductNO.Text;
                _report.ShippedTo = this.textEditShippedTo.Text;
                _report.TypeOfPack = this.textEditTypeOfPack.Text;
                _report.Weight = this.textEditWeight.Text;
                _report.LoginUserID = LoginUserUtil.LoginUser.LoginUserID;
                _report.ReportProductResults = _resList;
                db.Entry(_report).State = EntityState.Modified;
                bool res = db.SaveChanges() > 0;
                if (res)
                {
                    _ShowAllData();
                    XtraMessageBox.Show(ResourceOverall.EditSucceed);
                    this.Close();
                }
            }
        }

        private void searchLookUpEdit_Customer_EditValueChanged(object sender, EventArgs e)
        {
            InitEmptResultList();
            //自动填写地址
            if (this.searchLookUpEdit_Customer.EditValue != null || this.searchLookUpEdit_Customer.EditValue.ToString() != "")
            {
                string custID = Convert.ToString(this.searchLookUpEdit_Customer.EditValue);
                Customer customer = customerBll.LoadEntities(m => m.CustomerID == custID).FirstOrDefault();
                if (customer != null)
                {
                    this.textEditShippedTo.Text = customer.Address;
                }
            }
        }
        private void lookUpEditGoodsName_EditValueChanged(object sender, EventArgs e)
        {
            InitEmptResultList();
            if (_option == 1)
            {
                this.textEdit_ReportProductNO.Text = CreateReportNo(this.lookUpEditGoodsName.Text);
            }

        }

        //读取合成样的数据
        private void InitEmptResultList()
        {
            if (this.lookUpEditGoodsName.EditValue == null || this.lookUpEditGoodsName.EditValue.ToString() == "")
            {
                return;
            }
            if (this.searchLookUpEdit_Customer.EditValue == null || this.searchLookUpEdit_Customer.EditValue.ToString() == "")
            {
                return;
            }

            string goodsID = Convert.ToString(this.lookUpEditGoodsName.EditValue);
            string custID = Convert.ToString(this.searchLookUpEdit_Customer.EditValue);

            _resList = reportProductBll.GetEmptResult(goodsID, custID);
            this.dataGridControl.DataSource = _resList;

            //排序
            gridView1.BeginDataUpdate();
            try
            {
                gridView1.ClearSorting();
                gridView1.Columns["FormOrder"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            }
            finally
            {
                gridView1.EndDataUpdate();
            }
        }

        private void dateEdit_DateProduced_EditValueChanged(object sender, EventArgs e)
        {
            //if (this.lookUpEditGoodsName.EditValue == null || this.lookUpEditGoodsName.EditValue.ToString() == "")
            //{
            //    return;
            //}
            //if (this.searchLookUpEdit_Customer.EditValue == null || this.searchLookUpEdit_Customer.EditValue.ToString() == "")
            //{
            //    return;
            //}
            //if (this.dateEdit_DateProduced.EditValue == null || this.dateEdit_DateProduced.Text == "")
            //{
            //    return;
            //}
            //int goodsID = Convert.ToInt32(this.lookUpEditGoodsName.EditValue);
            //int custID = Convert.ToInt32(this.searchLookUpEdit_Customer.EditValue);
            //DateTime dateProduct = this.dateEdit_DateProduced.DateTime;
            //List<ReportProductResult> resList = reportProductBll.FillMixResult(goodsID, custID, dateProduct);
            //if (resList == null)
            //{
            //    XtraMessageBox.Show(dateProduct.ToShortDateString() + "有多份" + lookUpEditGoodsName.Text + "报告，请检查！");
            //}
            //else
            //{
            //    _resList = resList;
            //    this.dataGridControl.DataSource = _resList;
            //}
        }

        private void ButtonItem_addTestItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show("样品名称必选!The name of the sample must be selected !  ");
            }
            if (this.searchLookUpEdit_Customer.EditValue == null)
            {
                XtraMessageBox.Show("客户名称必选!The customer name must be selected !  ");
                return;
            }
            FlyHorse.LIMS.WinApp.OtherForms.AddResultForm addResultForm = new OtherForms.AddResultForm(AddTestItem);
            addResultForm.ShowDialog();
        }

        private void AddTestItem(TestItem testItem, string minValue, string maxVlaue)
        {
            ReportProductResult res = new ReportProductResult();
            res.Basis = testItem.Basis;
            res.ChineseName = testItem.ChineseName;
            res.FormOrder = testItem.Order;
            res.ReportProductResultID = Guid.NewGuid().ToString();
            res.MinValue = minValue;
            res.MaxValue = maxVlaue;
            res.TestItemID = testItem.TestItemID;
            res.Unit = testItem.Unit;
            _resList.Add(res);
            string name = this.dataGridControl.DataMember;
            dataGridControl.RefreshDataSource();
            this.dataGridControl.Refresh();
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            string id = (string)this.gridView1.GetRowCellValue(e.RowHandle, "ReportProductResultID");
            string minValue = null;
            string maxValue = null;
            string testValue = null;
            if (this.gridView1.GetRowCellValue(e.RowHandle, "MinValue") != null)
            {
                minValue = this.gridView1.GetRowCellValue(e.RowHandle, "MinValue").ToString();
            }
            if (this.gridView1.GetRowCellValue(e.RowHandle, "MaxValue") != null)
            {
                maxValue = this.gridView1.GetRowCellValue(e.RowHandle, "MaxValue").ToString();
            }
            if (this.gridView1.GetRowCellValue(e.RowHandle, "TestValue") != null)
            {
                testValue = this.gridView1.GetRowCellValue(e.RowHandle, "TestValue").ToString();
            }

            if (!String.IsNullOrEmpty(minValue))
            {
                Convert.ToDouble(this.gridView1.GetRowCellValue(e.RowHandle, "MinValue"));
                minValue = Convert.ToString(this.gridView1.GetRowCellValue(e.RowHandle, "MinValue"));
            }
            if (!String.IsNullOrEmpty(maxValue))
            {
                Convert.ToDouble(this.gridView1.GetRowCellValue(e.RowHandle, "MaxValue"));
                maxValue = Convert.ToString(this.gridView1.GetRowCellValue(e.RowHandle, "MaxValue"));
            }
            if (!String.IsNullOrEmpty(testValue))
            {
                try
                {
                    Convert.ToDouble(this.gridView1.GetRowCellValue(e.RowHandle, "TestValue"));
                    testValue = Convert.ToString(this.gridView1.GetRowCellValue(e.RowHandle, "TestValue"));
                }
                catch (Exception ex)
                {
                    this.gridView1.SetRowCellValue(e.RowHandle, "TestValue", null);
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
            }
            string res = FlyHorse.LIMS.WinApp.Utils.GradeJudging.GetResultByValue(minValue, maxValue, testValue);
            _resList.Where(m => m.ReportProductResultID == id).First().Conclusion = res;
            this.dataGridControl.Refresh();
        }

        private void ButtonItem_DelTestItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("ReportProductResultID") != null)
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    string id = (string)this.gridView1.GetFocusedRowCellValue("ReportProductResultID");
                    _resList = (List<ReportProductResult>)this.dataGridControl.DataSource;
                    ReportProductResult res = _resList.Where(m => m.ReportProductResultID == id).First();
                    DeleteReportResult(_resList, res);
                    this.dataGridControl.RefreshDataSource();
                }
            }
            else
            {
                XtraMessageBox.Show(ResourceOverall.SelectNull);
            }
        }

        private void ButtonItem_adData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //判断是否选择
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            if (this.searchLookUpEdit_Customer.EditValue == null || this.searchLookUpEdit_Customer.EditValue.ToString() == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }

            //FillDataForm from = new FillDataForm(FillDataBySemiReport);
            //from.ShowDialog();
            FlyHorse.LIMS.WinApp.DataManageForms.ReportSemiForms.ReportSemiMainForm form = new FlyHorse.LIMS.WinApp.DataManageForms.ReportSemiForms.ReportSemiMainForm(FillDataBySemiReport);
            form.Show();

        }

        private void ButtonItem_adData2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            if (this.searchLookUpEdit_Customer.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }

            FlyHorse.LIMS.WinApp.DataManageForms.ReportMixForms.ReportMixMainForm from = new FlyHorse.LIMS.WinApp.DataManageForms.ReportMixForms.ReportMixMainForm(FillDataByMixReport);
            from.Show();
        }


        private void FillDataBySemiReport(ReportSemi semiReport)
        {
            //检测样品是否一致
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            string goodsID = Convert.ToString(this.lookUpEditGoodsName.EditValue);
            string goodsName = lookUpEditGoodsName.Text;
            if (semiReport.GoodsID != goodsID)
            {
                DialogResult res = XtraMessageBox.Show(ResourceOverall.ConfirmFillErrName, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (res == DialogResult.Cancel)
                {
                    return;
                }
            }
            //判断生产日期是否一致
            if (dateEdit_DateProduced.EditValue == null)
            {
                dateEdit_DateProduced.Text = semiReport.SendDate.ToShortDateString();
            }
            else
            {
                if (dateEdit_DateProduced.DateTime != Convert.ToDateTime(semiReport.SendDate.ToShortDateString()))
                {
                    DialogResult res = XtraMessageBox.Show(ResourceOverall.ConfirmFillErrDate, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (res != DialogResult.OK)
                    {
                        return;
                    }
                }
            }

            if (_resList.Count == 0)
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmFillNullItem, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    foreach (var res in semiReport.ReportSemiResults)
                    {
                        ReportProductResult pResult = new ReportProductResult();
                        pResult.Basis = res.Basis;
                        pResult.ChineseName = res.ChineseName;
                        pResult.Conclusion = res.Conclusion;
                        pResult.FormOrder = res.FormOrder;
                        pResult.ReportProductResultID = Guid.NewGuid().ToString();
                        pResult.MaxValue = res.MaxValue;
                        pResult.MinValue = res.MinValue;
                        pResult.TestItemID = res.TestItemID;
                        pResult.TestValue = res.TestValue;
                        pResult.Unit = res.Unit;
                        _resList.Add(pResult);
                    }
                    this.dataGridControl.DataSource = _resList;
                    XtraMessageBox.Show(ResourceOverall.FillFinish);
                }
            }
            else
            {
                //先确定是否覆盖
                bool isCover = true;
                string name = "";
                foreach (var res in _resList)
                {
                    if (!String.IsNullOrEmpty(res.TestValue))
                    {
                        name += "," + res.ChineseName;
                    }
                }
                if (name != "")
                {
                    //DialogResult dialog= XtraMessageBox.Show(name + " \n 已经存在数据，是否覆盖？");

                    DialogResult dialog = XtraMessageBox.Show(name + " \n 已经存在数据，是否覆盖？", ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dialog == DialogResult.Cancel)
                    {
                        isCover = false;
                    }
                    if (dialog == DialogResult.OK)
                    {
                        isCover = true;
                    }
                }

                foreach (var result in semiReport.ReportSemiResults)   // 过程报告
                {
                    if (isCover)
                    {
                        foreach (var res in _resList)    //本报告
                        {
                            if (res.ChineseName == result.ChineseName)
                            {
                                if (!string.IsNullOrEmpty(result.TestValue))
                                {
                                    res.TestValue = result.TestValue;
                                    string conclusion = FlyHorse.LIMS.WinApp.Utils.GradeJudging.GetResultByValue(res.MinValue, res.MaxValue, res.TestValue);
                                    res.Conclusion = conclusion;
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var res in _resList)    //本报告
                        {
                            if (res.ChineseName == result.ChineseName)
                            {
                                if (!string.IsNullOrEmpty(result.TestValue))
                                {
                                    if (String.IsNullOrEmpty(res.TestValue))
                                    {
                                        res.TestValue = result.TestValue;
                                        string conclusion = FlyHorse.LIMS.WinApp.Utils.GradeJudging.GetResultByValue(res.MinValue, res.MaxValue, res.TestValue);
                                        res.Conclusion = conclusion; 
                                    }
                                }
                            }
                        }
                    }

                }
                this.dataGridControl.RefreshDataSource();
                XtraMessageBox.Show(ResourceOverall.FillFinish);
            }
        }


        private void FillDataByMixReport(ReportMix mixReport)
        {
            //检测样品是否一致
            if (this.lookUpEditGoodsName.EditValue == null)
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            string goodsID = Convert.ToString(this.lookUpEditGoodsName.EditValue);
            string goodsName = lookUpEditGoodsName.Text;
            if (mixReport.GoodsID != goodsID)
            {
                DialogResult res = XtraMessageBox.Show(ResourceOverall.ConfirmFillErrName, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (res == DialogResult.Cancel)
                {
                    return;
                }
            }
            //判断生产日期是否一致
            if (dateEdit_DateProduced.EditValue == null && mixReport.SendDate != null)
            {
                dateEdit_DateProduced.Text = Convert.ToDateTime(mixReport.SendDate).ToShortDateString();
            }
            else
            {
                if (mixReport.SendDate != null)
                {
                    if (dateEdit_DateProduced.DateTime != Convert.ToDateTime(mixReport.SendDate))
                    {
                        DialogResult res = XtraMessageBox.Show(ResourceOverall.ConfirmFillErrDate, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (res != DialogResult.OK)
                        {
                            return;
                        }
                    }
                }
            }

            if (_resList.Count == 0)
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmFillNullItem, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    foreach (var res in mixReport.ReportMixResults)
                    {
                        ReportProductResult pResult = new ReportProductResult();
                        pResult.Basis = res.Basis;
                        pResult.ChineseName = res.ChineseName;
                        pResult.Conclusion = res.Conclusion;
                        pResult.FormOrder = res.FormOrder;
                        pResult.ReportProductResultID = Guid.NewGuid().ToString();
                        pResult.MaxValue = res.MaxValue;
                        pResult.MinValue = res.MinValue;
                        pResult.TestItemID = res.TestItemID;
                        pResult.TestValue = res.TestValue;
                        pResult.Unit = res.Unit;
                        _resList.Add(pResult);
                    }
                    this.dataGridControl.DataSource = _resList;
                    XtraMessageBox.Show(ResourceOverall.FillFinish);
                }
            }
            else
            {
                //先确定是否覆盖
                bool isCover = true;
                string name = "";
                foreach (var res in _resList)
                {
                    if (!String.IsNullOrEmpty(res.TestValue))
                    {
                        name += "," + res.ChineseName;
                    }
                }
                if (name != "")
                {
                    DialogResult dialog = XtraMessageBox.Show(name + " \n 已经存在数据，是否覆盖？", ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dialog == DialogResult.Cancel)
                    {
                        isCover = false;
                    }
                    if (dialog == DialogResult.OK)
                    {
                        isCover = true;
                    }
                }

                ////////////////////

                foreach (var result in mixReport.ReportMixResults)   // 过程报告
                {
                    if (isCover)
                    {
                        foreach (var res in _resList)    //本报告
                        {
                            if (res.ChineseName == result.ChineseName)
                            {
                                if (!String.IsNullOrEmpty(result.TestValue))
                                {
                                    res.TestValue = result.TestValue;
                                    string conclusion = FlyHorse.LIMS.WinApp.Utils.GradeJudging.GetResultByValue(res.MinValue, res.MaxValue, res.TestValue);
                                    res.Conclusion = conclusion;
                                }
                                
                            }
                        }
                    }
                    else     //不覆盖
                    {
                        foreach (var res in _resList)    //本报告
                        {
                            if (!String.IsNullOrEmpty(result.TestValue))
                            {
                                if (String.IsNullOrEmpty(res.TestValue))
                                {
                                    if (res.TestItemID == result.TestItemID)
                                    {
                                        res.TestValue = result.TestValue;
                                        string conclusion = FlyHorse.LIMS.WinApp.Utils.GradeJudging.GetResultByValue(res.MinValue, res.MaxValue, res.TestValue);
                                        res.Conclusion = conclusion;
                                    } 
                                }
                            }

                        }
                    }

                }
                this.dataGridControl.RefreshDataSource();
                XtraMessageBox.Show(ResourceOverall.FillFinish);
            }
        }




        /// <summary>
        /// 删除检验结果
        /// </summary>
        /// <param name="reslutList"></param>
        /// <param name="res"></param>
        public void DeleteReportResult(List<ReportProductResult> reslutList, ReportProductResult res)
        {
            var entity = db.ReportProductResults.Where(m => m.ReportProductResultID == res.ReportProductResultID).FirstOrDefault();
            if (entity != null)
            {
                db.ReportProductResults.Remove(entity);
                db.SaveChanges();
                reslutList.Remove(res);
            }
            else
            {
                reslutList.Remove(res);
            }
        }


        /// <summary>
        /// 生成报告编号
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        private string CreateReportNo(string goodsName)
        {
            DateTime date = DateTime.Now;
            string reportNO = "";
            if (goodsName == "K5000" || goodsName == "XG5000")   //
            {
                reportNO += goodsName;
            }
            else
            {
                string firstName = goodsName.Substring(0, 1);
                if (firstName == "N")
                {
                    reportNO += goodsName.Substring(1, goodsName.Length - 1);
                }
                else
                {
                    reportNO += goodsName;
                }
            }

            reportNO += date.ToString("yyMMdd");
            DateTime dateStart = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
            DateTime dateEnd = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
            int count = db.ReportProducts.Where(m => m.AddDateTime >= dateStart && m.AddDateTime <= dateEnd).Count();

            reportNO += (count + 1).ToString("d2");


            return reportNO;
        }




    }
}